<template>
  <div>
    <layout-box one="权限管理" two="角色列表">
      <!-- <el-button type="primary" @click="dialogVisible = true"
        >添加角色</el-button
      > -->
      <!-- 角色列表区域 -->
      <el-table border stripe :data="roleList">
        <!-- <el-table-column type="expand">
          <el-row slot-scope="scope">
            <el-row
              :class="['bdbottom', i1 === 0 ? 'bdtop' : '', 'vcenter']"
              v-for="(item1, i1) in scope.row.children"
              :key="item1.id"
            >
              <el-col :span="5">
                <el-tag closable @close="removeRightById(scope.row, item1.id)">
                  {{ item1.authName }}</el-tag
                >
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <el-row
                  :class="[i2 === 0 ? '' : 'bdtop', 'vcenter']"
                  v-for="(item2, i2) in item1.children"
                  :key="item2.id"
                >
                  <el-col :span="6">
                    <el-tag
                      type="success"
                      closable
                      @close="removeRightById(scope.row, item2.id)"
                      >{{ item2.authName }}</el-tag
                    >
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      closable
                      @close="removeRightById(scope.row, item3.id)"
                      type="warning"
                      v-for="item3 in item2.children"
                      :key="item3.id"
                    >
                      {{ item3.authName }}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </el-row>
        </el-table-column> -->
        <el-table-column type="index"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <!-- <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini"
              >编辑
            </el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini"
              >删除</el-button
            >
            <el-tooltip
              effect="dark"
              content="分配权限"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="warning"
                icon="el-icon-setting"
                size="mini"
                @click="showSettingRight(scope.row)"
                >分配权限</el-button
              >
            </el-tooltip>
          </template>
        </el-table-column> -->
      </el-table>
    </layout-box>
    <!-- 分配权限对话框 -->
    <el-dialog
      title="提示"
      :visible.sync="rightsDialogVisible"
      @close="resetDefeyKeys"
    >
      <el-tree
        node-key="id"
        default-expand-all
        :data="rightslist"
        :props="treeProps"
        show-checkbox
        :default-checked-keys="defKeys"
        ref="treeRef"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="rightsDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="settingRights">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      roleList: [
        // {
        //   roleName: "管理员",
        //   roleDesc: "拥有所有权限",
        // },
        {
          roleName: "普通用户",
          roleDesc: "查、寄快递",
        },
        {
          roleName: "高管",
          roleDesc: "拥有所有权限",
        },
        {
          roleName: "职工",
          roleDesc: "更新物流信息",
        },
      ],
      rightsDialogVisible: false,
      rightslist: [],
      treeProps: {
        children: "children", //值是数据源上的属性
        label: "authName",
      },
      // 某一个角色下所有的三级权限
      defKeys: [],
      roleId: 0,
    };
  },
  created() {
    //获取角色列表
    this.getRolesList();
  },
  methods: {
    async getRolesList() {
      const { data: res } = await this.$http.get("/roles");
      if (res.meta.status != 200) {
        return this.$Message.error("获取角色列表失败");
      }
      //  console.log(res.data);
      this.roleList = res.data;
    },
    async removeRightById(role, rightsId) {
      const confirmResult = await this.$confirm(
        "此操作将永久删除该权限, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (confirmResult !== "confirm") {
        return this.$Message.info("取消了删除!");
      }
      const { data: res } = await this.$http.delete(
        `roles/${role.id}/rights/${rightsId}`
      );
      if (res.meta.status != 200) {
        return this.$Message.error("删除权限失败!");
      }
      this.$Message.success("删除权限成功");
      //将服务器返回的该角色下全新的权限赋值给role.children
      role.children = res.data;
    },
    // 分配权限
    async showSettingRight(role) {
      this.roleId = role.id;
      this.rightsDialogVisible = true;
      const { data: res } = await this.$http.get("rights/tree");
      if (res.meta.status !== 200) {
        return this.$Message.error("加载权限列表失败!");
      }
      this.rightslist = res.data;
      this.getDefKeys(role, this.defKeys);
    },
    // 获取角色下的三级权限
    //参数一 角色对象
    // 参数二 数组
    // 通过递归获取三级权限id
    getDefKeys(node, arr) {
      if (!node.children) {
        return arr.push(node.id);
      }
      node.children.forEach((item) => this.getDefKeys(item, arr));
    },
    resetDefeyKeys() {
      this.defKeys = [];
    },
    async settingRights() {
      // 获取所有的全选和板悬的节点，也就是权限ID
      // ...ES6的扩展运算符 就是展开数组 追加到新数组中
      let rightArr = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys(),
      ];
      const { data: res } = await this.$http.post(
        `roles/${this.roleId}/rights`,
        { rids: rightArr.join(",") }
      );
      if (res.meta.status != 200) {
        return this.$Message.error("修改权限失败");
      }
      this.$Message.success("修改权限成功");
      // 重新获取数据
      this.getRolesList();
      // 关闭弹窗
      this.rightsDialogVisible = false;
    },
  },
};
</script>
<style lang="less" scoped>
.el-table {
  margin-top: 15px;
}
.el-tag {
  margin: 7px;
}
.bdtop {
  border-top: 1px solid #eee;
}
.bdbottom {
  border-bottom: 1px solid #eee;
}
.vcenter {
  display: flex;
  align-items: center;
}
</style>